import { ref } from 'vue'
import { defineStore } from 'pinia'
import { showFailToast } from 'vant';
import {getCartList} from '@/apis/cart';

export const useCartStore = defineStore('cart', () => {
  const dataSource  = ref([]);
  const total = ref(0);
  const totalPrice = ref(0);

  const init =  async () => {
    try {
      const {data,code,msg} = await getCartList();
      if(code === 200){
        dataSource.value = data?.list || [];
        total.value = data?.totalQty || 0;
        totalPrice.value = data?.totalPrice || 0;
        return;
      }
      showFailToast(msg);
  } catch (error) {
    console.log(error,'get-cart-list-error');
  }
  }


  return {
    dataSource,
    total,
    totalPrice,
    init,
  }
})
